package com.example.springboot.service;

import com.example.springboot.common.Page;
import com.example.springboot.entity.User;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.UserMapper;
//import com.example.springboot.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
/**
 * 功能：
 * 作者：旺旺威化饼
 * 日期：2025/03/04 10:25
 **/
public class UserService {
    @Autowired
    UserMapper userMapper;
    public void insertUser(User user){
        userMapper.insert(user);
    }

    public void updateUser(User user) {
        userMapper.update(user);
    }

    public void deleteUser(Integer id) {
        userMapper.deleteUser(id);
    }

    public void batchDeleteUser(List<Integer> ids) {
        for (Integer id:ids){
            userMapper.deleteUser(id);
        }
    }

    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    public User selectById(Integer id) {
        return userMapper.selectById(id);
    }

    public List<User> selectByName(String name) {
        return userMapper.selectByName(name);
    }

    public List<User> selectByMore(String username, String name) {
        return userMapper.selectByMore(username,name);
    }

    public List<User> selectByVague(String username, String name) {
        return userMapper.selectByVague(username,name);
    }

    public Page<User> selectByPage(Integer pageNum, Integer pageSize, String username, String name) {
        Integer skipNum= (pageNum - 1 ) * pageSize;    //计算出来    例如：当pageSize:5时   1 ->0,5  2 ->5,5  3 ->10,5
        Page<User> page=new Page<>();
        List<User> userList= userMapper.selectByPage(skipNum, pageSize, username, name);
        Integer total=userMapper.selectCountByPage(username,name);
        page.setUserList(userList);
        page.setTotal(total);
        return page;
    }
    //验证用户账号是否合法
    public User login(User user) {
        //根据用户名查询用户信息
        User dbUser = userMapper.selectByUsername(user.getUsername());
        if (dbUser==null){
            //抛出一个自定义的异常
            throw new ServiceException("用户名或密码错误");
        }
        if (!user.getPassword().equals(dbUser.getPassword())){
            throw new ServiceException("用户名或密码错误");
        }
//        //生成 token
//        String token = TokenUtils.createToken(dbUser.getId().toString(), dbUser.getPassword());
//        dbUser.setToken(token);
        return dbUser;
    }

    public User register(User user) {
        User dbUser=userMapper.selectByUsername(user.getUsername());
        if(dbUser!=null){
            //抛出一个自定义的异常
            throw new ServiceException("用户名已存在");
        }
        user.setName(user.getUsername());
//        user.setName("honey-"+ RandomUtil.randomNumbers(4));      //如果重名可以在用户名后面生成随机数
        userMapper.insert(user);
        return user;
    }

    public void resetPassword(User user) {
        User dbUser = userMapper.selectByUsername(user.getUsername());
        if (dbUser == null){
            //抛出一个自定义的异常
            throw new ServiceException("用户不存在");
        }
        if(!user.getPhone().equals(dbUser.getPhone())){
            throw new ServiceException("验证错误");
        }
        dbUser.setPassword("123");  //重置密码
        userMapper.update(dbUser);
    }
}
